package net.es.weathermap.topo.network;

import java.util.*;

import org.apache.log4j.Logger;

import net.es.weathermap.util.ConfigHelper;

/**
 * Singleton to store OSCARS reservations.
 * @author David Robertson, Evangelos Chaniotakis
 */
public class ResvInfo {

    private static ResvInfo instance;
    private static final String DEFAULT_INFO_FILE = "resvs.yaml";

    private Logger log;
    private ArrayList<HashMap<String,String>> resvs;

    /**
     * Constructor - private because this is a Singleton
     */
    private ResvInfo() {
        this.log = Logger.getLogger(this.getClass());
    }

    /**
     * @return the initialized ResvInfo singleton instance
     */
    public static ResvInfo getInstance(boolean reset) {
        if (ResvInfo.instance == null) {
            ResvInfo.instance = new ResvInfo();
        }
        if (reset) {
            ResvInfo.instance.setResvs(DEFAULT_INFO_FILE);
        }
        return ResvInfo.instance;
    }

    public ArrayList<HashMap<String, String>> getReservations() {
        return this.resvs;
    }


    private void setResvs(String fname) {
        ConfigHelper cfg = ConfigHelper.getInstance();
        Map yamlMap = cfg.getConfiguration(fname);
        assert yamlMap != null:  "Could not load reservation info file from " +
            "file: ${basedir}/config/" + DEFAULT_INFO_FILE;
        this.resvs = (ArrayList<HashMap<String,String>>) yamlMap.get("reservations");
        assert this.resvs != null: "Reservations file does not have info section";
    }
}
